package cn.iocoder.yudao.module.shop.info.service;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.shop.info.dal.dataobject.ShopEnterPackageDO;
import cn.iocoder.yudao.module.shop.info.dal.mysql.ShopEnterPackageMapper;
import cn.iocoder.yudao.module.shop.info.vo.ShopEnterPackageSaveReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.shop.ErrorCodeConstants.ENTER_PACKAGE_NOT_EXISTS;

/**
 * 店铺入驻套餐 Service 实现类
 *
 * @author 山野羡民（1032694760@qq.com）
 */
@Service
@Validated
public class ShopEnterPackageServiceImpl implements ShopEnterPackageService {

    @Resource
    private ShopEnterPackageMapper shopEnterPackageMapper;

    @Override
    public Long createEnterPackage(ShopEnterPackageSaveReqVO createReqVO) {
        // 插入
        ShopEnterPackageDO enterPackage = BeanUtils.toBean(createReqVO, ShopEnterPackageDO.class);
        shopEnterPackageMapper.insert(enterPackage);
        // 返回
        return enterPackage.getId();
    }

    @Override
    public void updateEnterPackage(ShopEnterPackageSaveReqVO updateReqVO) {
        // 校验存在
        validateEnterPackageExists(updateReqVO.getId());
        // 更新
        ShopEnterPackageDO updateObj = BeanUtils.toBean(updateReqVO, ShopEnterPackageDO.class);
        shopEnterPackageMapper.updateById(updateObj);
    }

    @Override
    public void deleteEnterPackage(Long id) {
        // 校验存在
        validateEnterPackageExists(id);
        // 删除
        shopEnterPackageMapper.deleteById(id);
    }

    private void validateEnterPackageExists(Long id) {
        if (shopEnterPackageMapper.selectById(id) == null) {
            throw exception(ENTER_PACKAGE_NOT_EXISTS);
        }
    }

    @Override
    public ShopEnterPackageDO getEnterPackage(Long id) {
        return shopEnterPackageMapper.selectById(id);
    }

    @Override
    public ShopEnterPackageDO getOne() {
        return shopEnterPackageMapper.selectOne();
    }

    @Override
    public List<ShopEnterPackageDO> getList() {
        return shopEnterPackageMapper.selectList();
    }

    @Override
    public PageResult<ShopEnterPackageDO> getEnterPackagePage(PageParam pageReqVO) {
        return shopEnterPackageMapper.selectPage(pageReqVO);
    }

}
